#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

const int N = 1e5 + 10, M = 5e5 + 10;

int h[N], w[M], e[M], ne[M], dist[N], idx;
bool st[N];
int n, m, s;
void add(int a, int b, int c)
{
	e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
void spfa()
{
	memset(dist, 0x3f, sizeof  dist);
	dist[s] = 0;
	queue<int> q;
	q.push(s);
	st[s] = true;
	while (q.size())
	{
		int t = q.front();
		q.pop();
		st[t] = false;
		for (int i = h[t]; i != -1; i = ne[i])
		{
			int j = e[i];
			if (dist[j] > dist[t] + w[i])
			{
				dist[j] = dist[t] + w[i];
				if (!st[i])
				{
					q.push(j);
					st[j] = true;
				}
			}
		}
	}
	for (int i = 1; i <= n; i++)
		if (dist[i] == 0x3f3f3f3f) dist[i] = 2e31 - 1;
}
int main()
{
	cin >> n >> m >> s;
	memset(h, -1, sizeof h);
	while (m--)
	{
		int a, b, c;
		cin >> a >> b >> c;
		add(a, b, c);
	}
	spfa();
	for(int i = 1;i <= n;i ++)
		printf("%d ", dist[i]);
}